package com.cnpc.iot.mobile.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cnpc.framework.base.entity.SysFile;
import com.cnpc.framework.base.service.BaseService;
import com.cnpc.framework.base.service.UploaderService;
import com.cnpc.framework.utils.DateUtil;
import com.cnpc.framework.utils.PropertiesUtil;
import com.cnpc.framework.utils.SecurityUtil;
import com.cnpc.iot.mobile.controller.vo.ScglAdd;
import com.cnpc.iot.mobile.service.MobileService;
import com.cnpc.iot.scgl.entity.Pnode;
import com.cnpc.iot.scgl.utils.FileUtils;

@Service
public class MobileServiceImpl implements MobileService {
	private static final String uploaderPath = PropertiesUtil.getValue("uploaderPath");

	@Resource
	private BaseService baseService;
	@Resource
    private UploaderService uploaderService;

	@Override
	public void addScglData(String dirPath, ScglAdd scglAdd) {
		String files = "";
		String images1 = saveImage(dirPath, scglAdd.getImageData1(), "images1.png");
		if(images1.length() > 0) {
			files += images1;
		}
		String images2 = saveImage(dirPath, scglAdd.getImageData2(), "images2.png");
		if(images2.length() > 0) {
			files += ","+images2;
		}
		String images3 = saveImage(dirPath, scglAdd.getImageData3(), "images3.png");
		if(images3.length() > 0) {
			files += ","+images3;
		}
		Pnode pnode = baseService.get(Pnode.class, scglAdd.getNodeId());
		String table = "dny_" + pnode.getCode();
		StringBuffer sb = new StringBuffer("insert into " + table);
		sb.append(" (id,parent,creater,optime,fileids,operats)");
		sb.append(" values ('");
		sb.append(UUID.randomUUID().toString());
		sb.append("','");
		sb.append(scglAdd.getParent());
		sb.append("','");
		sb.append("billJiang-test");
		sb.append("','");
		sb.append(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
		sb.append("','");
		sb.append(files);
		sb.append("','");
		sb.append(scglAdd.getOperator());
		sb.append("')");
		baseService.executeSql(sb.toString());
	}

	private String saveImage(String dirPath, String images, String imgName) {
		String filePrefixFormat="yyyyMMddHHmmssS";
        String savedName=DateUtil.format(new Date(),filePrefixFormat)+"_"+imgName;
        File dir = new File(dirPath+uploaderPath);
        if (!dir.exists()) {
        	dir.mkdirs();
        }
        String filePath=dir.getAbsolutePath() + File.separator + savedName;
        File serverFile = new File(filePath);

		try {
			MultipartFile mf = FileUtils.base64ToMultipart(images);
			mf.transferTo(serverFile);
			
			SysFile sysFile=new SysFile();
            sysFile.setFileName(imgName);
            sysFile.setSavedName(savedName);
            sysFile.setCreateDateTime(new Date());
            sysFile.setUpdateDateTime(new Date());
//            sysFile.setCreateUserId(SecurityUtil.getUserId());
            sysFile.setCreateUserId("402880e456223e8f015625231d8e0003");
            sysFile.setDeleted(0);
            sysFile.setFileSize(mf.getSize());
            sysFile.setFilePath(uploaderPath+File.separator+savedName);
            uploaderService.save(sysFile);
			return sysFile.getId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

}
